package com.piesat.quake.common.websocket.interceptors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.stereotype.Component;

@Component
public class ConnWebSocketInterceptor implements ChannelInterceptor {

    //这里使用的消息模板，注意，这里的消息模板需要等上面的配置类配置结束之后才能使用
    @Autowired
    @Lazy
    private SimpMessagingTemplate messagingTemplate;

    //这里实现的是在客户端与服务端建立连接后，直接发送一条信息给客户端
    @Override
    public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {
        messagingTemplate.convertAndSend("/topic/connect", "Hello Client");
    }
}
